﻿// 版权所有：
// 文 件  名：ThreadTask.cs
// 功能描述：
// 创建标识：Seven Song(m.sh.lin0328@163.com) 2014/3/11 22:23:54
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Text;
using System.Threading;

namespace MSL.Utility.Threading
{
    /// <summary>
    /// 线程任务
    /// </summary>
    public class ThreadTask
    {
        #region 变量/属性

        private Int32 _id;
        private WaitCallback _method;
        private Object _argument;
        private WaitCallback _abortMethod;
        private static Object _Lock = new object();
        private static Int32 _newID;

        /// <summary>
        /// 唯一编号
        /// </summary>
        public Int32 ID
        {
            get { return _id; }
            private set { _id = value; }
        }

        /// <summary>
        /// 任务方法
        /// </summary>
        public WaitCallback Method
        {
            get { return _method; }
            set { _method = value; }
        }

        /// <summary>
        /// 任务参数
        /// </summary>
        public Object Argument
        {
            get { return _argument; }
            set { _argument = value; }
        }
        /// <summary>
        /// 取消任务时执行的方法
        /// </summary>
        public WaitCallback AbortMethod
        {
            get { return _abortMethod; }
            set { _abortMethod = value; }
        }

        /// <summary>
        /// 取一个新编号
        /// </summary>
        private static Int32 NewID
        {
            get
            {
                lock (_Lock)
                {
                    _newID++;
                    return _newID;
                }
            }
        }
        #endregion

        /// <summary>
        /// 构造一个线程任务
        /// </summary>
        /// <param name="method">任务方法</param>
        /// <param name="argument">任务参数</param>
        public ThreadTask(WaitCallback method, Object argument)
        {
            Method = method;
            Argument = argument;
            ID = NewID;
        }

        /// <summary>
        /// 构造一个线程任务
        /// </summary>
        /// <param name="method">任务方法</param>
        /// <param name="abortMethod">任务被取消时执行的方法</param>
        /// <param name="argument">任务参数</param>
        public ThreadTask(WaitCallback method, WaitCallback abortMethod, Object argument)
        {
            Method = method;
            Argument = argument;
            ID = NewID;
            AbortMethod = abortMethod;
        }
    }
}
